About 10141 letters

About 51 minutes

#Python's basic operations

Arithmetic is playing around with values, like 1 + 1 gives 2, where + is the operator for addition.

For more information about operators, please refer to Python official documentation

#Assignment operator

Python uses = as an assignment operator to modify the value of a variable, not "equals" in mathematics.
You can also use count += 1 as a shorthand for count = count + 1, where + can be replaced by any arithmetic operator.

count:int = 0 # Create a variable count with a value of 0 print(count) count = 4 # Change the value of count to 4 print(count) count += 3 # Change the value of count to count + 3 print(count)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

#Dynamic type

After a variable is created in Python, it can be assigned other types of values:

age_of_yukari:int = 17 # type is int age_of_yukari = "17" # type is str

However, it is recommended not to modify the type of the variable.

#Walrus operator

The walrus operator (:=) is a special assignment operator that cannot be used to assign values ​​to variables directly, but can only be used within expressions. For example:

x:int = 0 y:int = 0 sum_squares:int = x**2 + y**2 # traditional practice x = 5 y = 10 sum_squares = x**2 + y**2 # use walrus operator sum_squares = (x := 5)**2 + (y := 10)**2

Also, the assignment operator (=) does not return a value and cannot be used as a condition for control blocks such as if and while, while the walrus operator (:=) can.

#Arithmetic operators

operatorsnameexampleresultillustrate
+addition operator3 + 57
-subtraction operator5 - 32
*multiplication operator5 * 315
/division operator5 / 22.5regardless of whether it is divisible or not, the result type is floating point
//integer division operator5 // 2 2the result is rounded down. If both the dividend and the divisor are integers, the result is an integer, otherwise the result is a floating point.
%remainder operator5 % 215 divided by 2 equals 2 with a remainder of 1
**exponentiation operator5 ** 2255 to the power of 2

#Comparison operator

Used to compare the relationship between two values, and the result is boolean type.

operatorsnameexampleresult
==equals operator3 == 5False
!=not equal to operator3 != 5True
>greater than operator5 > 3True
<less than operator5 < 2False
>=greater than or equal to operator5 >= 5 True
<=less than or equal to operator5 <= 2False

#Logical operators

Used to combine multiple boolean values.

operatorsnameexampleresultillustrate
andlogical AND operatorTrue and FalseFalseThe result is True if all are True, otherwise the result is False
orlogical OR operatorTrue or FalseTrueThe result is False if all are False, otherwise the result is True
notlogical NOT operatornot FalseTrueInvert a noolean value

#Bit operators

Bitwise operations are used to perform binary operations on integers.

operatorsnameexampleresultillustrate
&bitwise AND operator0b1100 & 0b01100b0100Calculate by binary digit. If both numbers have 1 in a certain digit, the result is 1 in that digit. Otherwise, the result is 0 in that digit.
|bitwise or operator0b1100 | 0b01100b1110Calculate by binary digit. If both numbers are 0 in a certain digit, the result is 0 in that digit. Otherwise, the result is 1 in that digit.
^bitwise XOR operator0b1100 ^ 0b01100b1010Different bits in binary result in 1, and the same bits result in 0
<<left shift operator0b0011 << 20b1100Shift the binary bit to the left and fill the right side with 0
>>right shift operator0b1100 >> 10b0110Shift the binary bit to the right and fill the left side with 0
~negation operator~0b11000b0011Invert a binary bit, i.e., 1 becomes 0, and 0 becomes `1

#Priority

Operators have a priority, just like in mathematics, multiplication and division are performed first, and addition and subtraction are performed later. For example, results in .
Parentheses have the highest priority, and you can use them to change the order of calculation. For example, results in .

The following table is a list of precedences, from high to low:

Reference: Python official documentation

operatorsillustratepriority
(exp), [exp], {exp}Binding or parenthesized expression, list display, dictionary display, set displayhighest
arr[i], arr[i:j], func(args), obj.attrSubscription, slicing, call, attribute reference
await expAwait expression
**Exponentiation
+num, -num, ~numPositive, negative, bitwise NOT
*, @, /, //, %Multiplication, matrix multiplication, division, floor division, remainder
+, -Addition and subtraction
<<, >>Shifts
&Bitwise AND
^Bitwise XOR
|Bitwise OR
in, not in, is, is not, <, <=, >, >=, !=, ==Comparisons, including membership tests and identity tests
not expBoolean NOT
andBoolean AND
orBoolean OR
if – elseConditional expression
lambdaLambda expression
:=Assignment expressionlowest

#Practise

Please calculate the circumference and area of ​​a circle.

  • The formula for the circumference of a circle is

  • The formula for the area of ​​a circle is

PI:float = 3.141592 radius:float = 15 perimeter:float = 0 # Modify the code here to calculate the circumference area:float = 0 # Modify the code here to calculate the area print("The perimeter and area of ​​a circle with a radius of", radius, "are", perimeter, "and", area)

>>> Establishing WebAssembly Runtime.

>>> Standby.

Powered by Shift.

Created in 5/15/2025

Updated in 5/21/2025